package entities;

import android.annotation.SuppressLint;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;

import org.xmlpull.v1.XmlPullParserException;


@SuppressLint("NewApi")
public class Fact extends ReadXMLFile{

	
	public static final String xmlFileName = "Facts.xml";
	
	
	private static List<Fact> facts = null;

	private int id;

	private List<Integer> heroid;
	
	private List<Integer> itemid;
	
	private boolean isGen;
	
	private static final int LIST_ID = 0;

	private static final int LIST_HEROID = 1;

	private static final int LIST_ITEMID = 2;
	
	public int getId() {
		return id;
	}
	
	public List<Integer> getHeroid() {
		return heroid;
	}



	public List<Integer> getItemid() {
		return itemid;
	}



	public boolean isGen() {
		return isGen;
	}



	
	@Override
	protected void init(InputStream in) {
		tagName = "fact";
		branchName = "facts";
		fieldNames = new String[]{"id", "heroid", "itemid"};
		try {
			fillFacts(in);
		} catch (XmlPullParserException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}
	
	public Fact(InputStream in){
		init(in);
	}
	
	@SuppressLint("NewApi")
	private void fillFacts(InputStream in) throws XmlPullParserException, IOException {
		if(facts==null){
			@SuppressWarnings("unchecked")
			List<List<String>> list = parse(in);
			facts = new ArrayList<Fact>();
			for(List<String> l: list){
				String[] sh = l.get(LIST_HEROID).split(",");
				List<Integer> tempHl = new ArrayList<Integer>(); 
				
				for(String s: sh){
					if(!s.isEmpty()){
					tempHl.add(Integer.valueOf(s));
					}
				}
				List<Integer> tempIl = new ArrayList<Integer>(); 

				String[] si = l.get(LIST_ITEMID).split(",");
				for(String s: si){
					if(!s.isEmpty()){
					tempIl.add(Integer.valueOf(s));
					}
				}
				facts.add(new Fact(Integer.valueOf(l.get(LIST_ID)), tempHl, tempIl));
			}
			
			}
		
	}

	
	Fact(int i, List<Integer> h, List<Integer> it){
		id = i;
		heroid = h;
		itemid = it;
	}
	
	




	public static Fact getARandomFact(){
		 Random gerador = new Random();
		 
	     int r = gerador.nextInt(facts.size());
				
	     return facts.get(r);
	}




	
}